﻿using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
using PlanningGIS.Util.Management;
using PlanningGIS.Util.Log;
using System.Security.Permissions;
using PlanningGIS.Util.IO;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 概述：ArcGIS AO 帮助类<br />
    /// </summary>
    public static class AoHelper
    {        
        /// <summary>
        /// AO 初始化接口。
        /// </summary>
        private static AoInitialize _aoInitialize;

        /// <summary>
        /// 初始化许可。
        /// </summary>
        /// <returns>是否初始化成功。</returns>
        public static bool Initialize()
        {
            if (_aoInitialize == null)
            {
                RuntimeManager.BindLicense(ProductCode.EngineOrDesktop, LicenseLevel.GeodatabaseUpdate);
                _aoInitialize = new AoInitialize();
                if (_aoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) != esriLicenseStatus.esriLicenseAvailable)
                {
                    return false;
                }
                _aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
            }
            return true;
        }

        /// <summary>
        /// 关闭。
        /// </summary>
        public static void Shutdown()
        {
            if (_aoInitialize != null)
            {
                _aoInitialize.Shutdown();
                _aoInitialize = null;
            }
        }

        #region 配置文件
        /// <summary>
        /// 用户临时配置文件
        /// </summary>
        public static string _UserConfig
        {
            get { return PathHelper.GetAppPath() + "\\Temp\\UserCfg.ini"; }
        }

        /// <summary>
        /// 
        /// </summary>
        private static string _StylePath = PathHelper.GetAppSettingPath("Folder") + "\\Style";

        /// <summary>
        /// 获取或设置符号路径，默认为运行程序下的Style目录
        /// </summary>
        public static string StylePath
        {
            get { return _StylePath; }
            set { _StylePath = value; }
        }

        /// <summary>
        /// 本地sde文件存放的路径
        /// </summary>
        private static string _SdePath = "";

        /// <summary>
        /// 本地sde文件存放的路径
        /// </summary>
        public static string SdePath
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_SdePath) || string.IsNullOrEmpty(_SdePath))
                {
                    string tmp = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ESRI";
                    if (Directory.Exists(tmp + @"\Desktop10.5"))
                    {
                        _SdePath = tmp + @"\Desktop10.5\ArcCatalog\";
                    }
                    else if (Directory.Exists(tmp + @"\Engine10.5"))
                    {
                        _SdePath = tmp + @"\Engine10.5\ArcCatalog\";
                    }
                    else
                    {
                        _SdePath = tmp + @"\Desktop10.5\ArcCatalog\";
                    }
                }
                return _SdePath;
            }
        }
        #endregion

    }
}